package com.example.dllo.my36kr.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.SearchBean;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 16/8/24.
 */
public class SearchAllAdapter extends BaseAdapter{
    private List<SearchBean.DataBean.DataBean1> dataBean1s;
    private Context context;

    public SearchAllAdapter(Context context) {
        this.context = context;
    }

    public void setDataBean1s(List<SearchBean.DataBean.DataBean1> dataBean1s) {
        this.dataBean1s = dataBean1s;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
           return dataBean1s != null ? dataBean1s.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataBean1s != null ? dataBean1s.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchHolder searchHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
            searchHolder = new SearchHolder(convertView);
            convertView.setTag(searchHolder);
        } else {
            searchHolder = (SearchHolder) convertView.getTag();
        }
        searchHolder.tv_tag.setText(dataBean1s.get(position).getColumnName());
        searchHolder.tv_title.setText(dataBean1s.get(position).getTitle());
        searchHolder.tv_writer.setText(dataBean1s.get(position).getUser().getName());
        Long time = dataBean1s.get(position).getPublishTime();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String formatTime = format.format(new Date(time));
        searchHolder.tv_time.setText(formatTime);
        Picasso.with(context).load(dataBean1s.get(position).getFeatureImg()).resize(70, 70).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher).into(searchHolder.img_news);

        return convertView;
    }

    class SearchHolder {
        private ImageView img_news;
        private TextView tv_title, tv_writer, tv_time, tv_tag;

        public SearchHolder(View view) {
            img_news = (ImageView) view.findViewById(R.id.img_ls_news);
            tv_title = (TextView) view.findViewById(R.id.tv_title_news);
            tv_writer = (TextView) view.findViewById(R.id.tv_writer_news);
            tv_time = (TextView) view.findViewById(R.id.tv_time_news);
            tv_tag = (TextView) view.findViewById(R.id.tv_tag_news);
        }
    }
}
